package com.zxlfysj.hashtable;

/**
 * 哈希表
 *
 * @author yangshujing
 * @create 2020-08-19 15:20
 */
public class HashTableDemo {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        Stu stu1 = new Stu(1, "百里");
        Stu stu2 = new Stu(7, "李白");
        Stu stu3 = new Stu(8, "宫本");
        hashTable.add(stu1); //测试添加
        hashTable.add(stu2);
        hashTable.add(stu3);
        hashTable.list(); //测试遍历
        //测试查找
        hashTable.findStuById(1);


    }

}

//创建哈希表，管理多条链表
class HashTable {
    private StuLinkedList[] stuLinkedLists;
    private int size;

    public HashTable(int size) {
        this.size = size;
        this.stuLinkedLists = new StuLinkedList[size];
        //初始化每条链表
        for (int i = 0; i < size; i++) {
            stuLinkedLists[i] = new StuLinkedList();
        }
    }

    //添加
    public void add(Stu stu) {
        int stuLinkedListNo = hashFun(stu.id);
        stuLinkedLists[stuLinkedListNo].add(stu);

    }

    //遍历哈希表
    public void list() {
        for (int i = 0; i < size; i++) {
            stuLinkedLists[i].list(i + 1);
        }
    }

    //根据id进行查找
    public void findStuById(int id) {
        int stuLinkedListNo = hashFun(id);
        Stu stu = stuLinkedLists[stuLinkedListNo].findStuById(id);
        if (stu != null) {
            System.out.printf("在第%d条链表中找到stu id=%d\n", stuLinkedListNo + 1, stu.id);
        } else {
            System.out.println("没有找到");
        }
    }

    //散列函数
    public int hashFun(int id) {
        return id % size;
    }
}

//创建链表
class StuLinkedList {
    private Stu head; //头指针，直接指向第一个Stu

    //添加
    public void add(Stu stu) {
        if (head == null) {
            head = stu;
            return;
        }
        Stu cur = head;
        while (true) {
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        cur.next = stu;
    }

    //遍历
    public void list(int no) {
        if (head == null) {
            System.out.println("第" + no + "条链表为空");
            return;
        }
        Stu cur = head;
        System.out.print("第" + no + "条链表信息：");
        while (true) {
            System.out.printf(cur + "=>");
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        System.out.println();
    }

    //根据id查找
    public Stu findStuById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Stu cur = head;
        while (true) {
            if (cur.id == id) {
                break;
            }
            if (cur.next == null) {
                cur = null;
                break;
            }
            cur = cur.next;
        }
        return cur;
    }
}

class Stu {
    public int id;
    public String name;
    public Stu next;

    public Stu(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
